/*-*
 * FILENAME  :
 *    $HeadURL$
 *
 * STATUS  :
 *    $Revision$
 *
 *    $Author$
 *    $Date$
 *  
 *    
 * Copyright (c) 2011 Gonalge Ltda. All rights reserved.
 *
 ****************************************************************/

package org.gonalge.rompecabeza.pintado;

import javax.swing.JPanel;
import javax.swing.Timer;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Area;
import java.awt.image.BufferedImage;

import org.gonalge.rompecabeza.servidor.FormOpciones;
import org.gonalge.rompecabeza.util.FormUtils;

/**
 * 
 * @author Geremias Gonzalez
 */
public class PintarOpciones extends JPanel implements MouseListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1497598754634976185L;

    private final BufferedImage imgA, imgB, imgC, imgD;

    private FormOpciones opciones = null;

    private boolean sw = true;

    private int x = 0, y = 0;

    private String imagenes = "Ardilla";

    private final Timer tim = new Timer(200, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            sw = !sw;
            repaint();
        }

    });

    public PintarOpciones(FormOpciones opj) {

        this.opciones = opj;

        imgA = FormUtils.getImagen(getClass(), "1");
        imgB = FormUtils.getImagen(getClass(), "2");
        imgC = FormUtils.getImagen(getClass(), "3");
        imgD = FormUtils.getImagen(getClass(), "4");
        tim.start();

        this.addMouseListener(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setFont(new Font("Arial", 1, 15));

        g.drawImage(imgA, 0, 0, getWidth() / 2, getHeight() / 2, this);
        g.drawImage(imgB, getWidth() / 2, 0, getWidth() / 2, getHeight() / 2, this);
        g.drawImage(imgC, 0, getHeight() / 2, getWidth() / 2, getHeight() / 2, this);
        g.drawImage(imgD, getWidth() / 2, getHeight() / 2, getWidth() / 2, getHeight() / 2, this);

        if (sw) {
            Area a = new Area(new Rectangle(x, y, getWidth() / 2, getHeight() / 2));
            a.subtract(new Area(new Rectangle(x + 10, y + 10, getWidth() / 2 - 20, getHeight() / 2 - 20)));
            g.setColor(Color.ORANGE);
            ((Graphics2D) g).fill(a);
            g.setColor(Color.BLACK);
            ((Graphics2D) g).draw(a);
        }

        g.drawString(imagenes, x + 30, y + 40);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getX() <= getWidth() / 2 && e.getY() <= getHeight() / 2) {
            x = 0;
            y = 0;
            imagenes = " ARDILLA ";
            opciones.setImagenDibujo(1);
        } else if (e.getX() <= getWidth() && e.getY() <= getHeight() / 2) {
            x = getWidth() / 2;
            y = 0;
            imagenes = "  GATO  ";
            opciones.setImagenDibujo(2);
        } else if (e.getX() <= getWidth() / 2 && e.getY() <= getHeight()) {
            x = 0;
            y = getHeight() / 2;
            imagenes = " PRINCESA";
            opciones.setImagenDibujo(3);
        } else {
            x = getWidth() / 2;
            y = getHeight() / 2;
            imagenes = "  MARIO  ";
            opciones.setImagenDibujo(4);
        }
    }

    public void setImagen(int img) {
        switch (img) {
            case 1: {
                x = 0;
                y = 0;
            }
                break;
            case 2: {
                x = getWidth() / 2;
                y = 0;
            }
                break;
            case 3: {
                x = 0;
                y = getHeight() / 2;
            }
                break;
            case 4: {
                x = getWidth() / 2;
                y = getHeight() / 2;
            }
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
